/**
 * Created by HaiHui on 2017/10/26.
 */
/**
 * Created by HaiHui on 2017/10/25.
 */
/**
 * Created by HaiHui on 2017/10/17.
 */
var table;
$(document).ready(function() {
    table = $('#information').DataTable({
        "pagingType": "simple_numbers", //设置分页控件的模式
        searching: false, //屏蔽datatales的查询框
        aLengthMenu: [10], //设置一页展示10条记录
        "bLengthChange": false, //屏蔽tables的一页展示多少条记录的下拉列表
        "ordering": false, //屏蔽排序
        "oLanguage": { //对表格国际化
            "sLengthMenu": "每页显示 _MENU_条",
            "sZeroRecords": "没有找到符合条件的数据",
            //  "sProcessing": "&lt;img src=’./loading.gif’ /&gt;",
            "sInfo": "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
            "sInfoEmpty": "木有记录",
            "sInfoFiltered": "(从 _MAX_ 条记录中过滤)",
            "sSearch": "搜索：",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前一页",
                "sNext": "后一页",
                "sLast": "尾页"

            }
        },
        "processing": true, //打开数据加载时的等待效果
        "serverSide": true, //打开后台分页
        "ajax": {
            "url": "dust/deadInfoByGrave",
            "dataSrc": "aaData",
            "data": function(d) {
                d.deadStatus = "3";
                d.buryMode = "1";
            }
        },
        "columns": [{
            "data": "baseId",
            "visible": false
        },
            {
                "data":"dustId",
                "visible":false
            },
            {
                "data":"burySite",
                "visible":false
            },
            {
                "data": "deadName",
                "title": "逝者姓名"
            },
            {
                "data": "deadCardNo",
                "title": "身份证号"
            },
            {
                "data": "deadTime",
                "title": "死亡时间"
            },
            {
                "data": "deadCremationDate",
                "title": "火化时间"
            },
            {
                "data":"familyName",
                "title":"亲属姓名"
            },
            {
                "data":"familyPhone",
                "title":"联系方式"
            },
            /*  {
             "data":"dustName",
             "title":"公墓名称"
             },*/
            {
                "data": null,
                "title": "操作"
            }
        ],
        "columnDefs": [{
            "targets": -1, //操作按钮目标列
            "data": null,
            "defaultContent": "<i title=\"合葬\" class=\"dogo-js-edit fa fa-user-plus\"></i>"

        }],
    });
    //查看
    $('#information tbody').on('click', '.dogo-js-edit', function() {
        var data = table.row($(this).parents('tr')).data();
        showEditDetail(data)
        // $('#search-modal').modal(data.prId);
    });


});

function search() {
    table.ajax.reload();

}
function showEditDetail(data) {
    var baseId = GetQueryString("baseId");
    $("#hideBaseId").val(baseId);
    $("#hideSecondId").val(data.baseId);
    $("#hideDustId").val(data.dustId);
    $("#hideGraveId").val(data.burySite);

    var setting = {
        url: "dust/setGraveDetail",

        data: {
            id: data.baseId
        }
    }
    ajaxSubmit(setting, function(responseData) {
        checkSubmitResult(responseData, {
            error: "获取信息登记失败"
        }, function(responseData) {
            //TODO 详情页面数据填充
            var deadInfo = responseData.data.deadInfo;
            var regionInfo = responseData.data.region;


            $("#second-modal").find("#infoDeadName").text(deadInfo.deadName);
            $("#second-modal").find("#infoDeadSex").text(deadInfo.dustSex);
            $("#second-modal").find("#infoDeadBirthday").text(deadInfo.deadBirthday);
            $("#second-modal").find("#infoDeadCardNo").text(deadInfo.deadCardNo);
            $("#second-modal").find("#infoDeadNation").text(deadInfo.deadNation);
            $("#second-modal").find("#infoDeadGroup").text(deadInfo.deadGroup);
            $("#second-modal").find("#infoDeadAddress").text(deadInfo.deadAddress);
            $("#second-modal").find("#infoDeadTime").text(deadInfo.deadTime);
            $("#second-modal").find("#infoDeadRemark").text(deadInfo.deadRemark);
            $("#second-modal").find("#infoHopeBuryAddress").text(deadInfo.hopeBuryAddress);
            $("#second-modal").find("#infoDustName").text(deadInfo.dustName);
            $("#second-modal").find("#infoDeadCremationDate").text(deadInfo.deadCremationDate);
            $("#second-modal").find("#infoFamilyName").text(deadInfo.familyName);
            $("#second-modal").find("#infoFamilyRelateive").text(deadInfo.familyRelateive);
            $("#second-modal").find("#infoFamilyPhone").text(deadInfo.familyPhone);
            $("#second-modal").find("#infoBurySiteName").text(deadInfo.burySiteName);

            //分配墓穴名称


            $("#second-modal").modal('show');
        });
    }, "获取信息登记失败");
}

function  saveGraveInfo(){

    var dustId = $("#hideDustId").val();
    var baseId=$("#hideBaseId").val();
    var graveId=$("#hideGraveId").val();
    var secondBaseId = $("#hideSecondId").val();

    var graveName=$("#infoBurySiteName").text();
    var confirmIdx = layer.confirm("确认要保存吗?", {
            btn: ['确定', '取消']
        },
        function() {
            var setting = {
                url: "dust/saveGraveInfo",
                data: {
                    baseId:baseId,
                    dustId:dustId,
                    graveId:graveId,
                    graveName:graveName,
                    type:"2",
                    secondBaseId:secondBaseId

                },
                dataType: 'json'

            }
            ajaxSubmit(setting, function(responseData) {
                checkSubmitResult(responseData, {
                    error: "删除失败"
                }, function(responseData) {
                    layer.alert(responseData.description+":"+graveName);
                    $("#second-modal").modal('hide');
                    table.ajax.reload();

                });
            }, "删除失败");
        },
        function() {
            layer.close(confirmIdx);
        });


}
function GetQueryString(name)
{
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}